var Page = function(){
    this.init();
};
Page.prototype = {
    params: {
        passport: '',
        imgUrl:'',
        slideEdit:0,
        shufflingId:'',
    },
    init: function(){
        this.initParams();
        this.initEvent();
    },
    initParams: function(){
        this.params.passport = Pub.getCookie('_PASSPORT');
        this.params.slideEdit = Pub.getSession('slideEdit');
        this.params.shufflingId = Pub.getSession('shufflingId');
    },
    getData:function(){
        var _this = this;
        var paramsObj = {
            passport: _this.params.passport,
            shufflingId:_this.params.shufflingId
        };
        Api.shufflingfiguredetails(paramsObj, function(data){
            console.log(data);
            $('#name').val(data.name);
            $('#typeSelect').val(data.type)
            $('#start_time').val(data.beginTime);
            $('#end_time').val(data.closeTime);
            _this.params.imgUrl=data.imgUrl;
            $('#adUrl').val(data.linkUrl);
            $('#adRemark').val(data.remark);
            if(data.isOn==1) {
                $('input[type=radio][name=line]').eq(0).iCheck('check');
            }else{
                $('input[type=radio][name=line]').eq(1).iCheck('check');
            };
            $('#slide-title').text('轮播图编辑')
        });
    },
    uploadImg: function(){
        var _this = this;
        Api.uploadLimitMore('#up_form1','#file_1', 'bannerimgurl',_this.params.passport, function(response){
            if(response.code === 0){
                console.log(response);
                _this.params.imgUrl=response.data.imgUrl;
                layui.use('layer', function(){
                    layer.msg('上传成功', {icon: 6});
                });
            }
        });
    },
    submit:function(){
        var _this = this;
        $('#submit').click(function(event) {

            var name=$('#name').val(),
                beginTime=$('#start_time').val(),
                closeTime=$('#end_time').val(),
                image=_this.params.imgUrl,
                linkUrl=$('#adUrl').val(),
                remark=$('#adRemark').val(),
                isOnline=$('input[type=radio][name=line]:checked').val();
            var paramsObj = {
                passport: _this.params.passport,
                name:name,
                type:$('#typeSelect').val(),
                beginTime:beginTime,
                closeTime:closeTime,
                isOn:isOnline,
                imgUrl:image,
                linkUrl:linkUrl,
                remark:remark
            };
            var Obj = {
                passport: _this.params.passport,
                shufflingId:_this.params.shufflingId,
                name:name,
                type:$('#typeSelect').val(),
                beginTime:beginTime,
                closeTime:closeTime,
                isOn:isOnline,
                imgUrl:image,
                linkUrl:linkUrl,
                remark:remark
            };
            if (name=='') {
                layui.use('layer', function(){
                    layer.msg('轮播图名称不能为空', {icon: 5});
                });
            }else if(beginTime==''){
                layui.use('layer', function(){
                    layer.msg('开始时间不能为空', {icon: 5});
                });
            }else if(closeTime==''){
                layui.use('layer', function(){
                    layer.msg('到期时间不能为空', {icon: 5});
                });
            }else if(Pub.toUnix(beginTime)>Pub.toUnix(closeTime)){
                layui.use('layer', function(){
                    layer.msg('开始时间不能大于到期时间', {icon: 5});
                });
            }else{
                console.log(paramsObj);
                if (_this.params.slideEdit==1) {
                    Api.shufflingfiguremodify(Obj, function(data){
                        console.log(data);
                        layui.use('layer', function(){
                            layer.msg('编辑成功', {icon: 6});

                        });
                        setTimeout(function(){
                            window.location.href=Pub.getHtmlUrl('./slide-set.html');
                        },1500)
                    });
                }else{
                    Api.shufflingfigureadd(paramsObj, function(data){
                        console.log(data);
                        layui.use('layer', function(){
                            layer.msg('添加成功', {icon: 6});

                        });
                        setTimeout(function(){
                            window.location.href=Pub.getHtmlUrl('./slide-set.html');
                        },1500)
                    });
                };

            };

        });

    },
    otherEvent:function(){
        var _this = this;
         $('.mx-radio input').iCheck({
            checkboxClass: 'icheckbox-blue',
            radioClass: 'iradio-blue',
            increaseArea: '20%'
        })

        layui.use('laydate', function(){
          var laydate = layui.laydate;
          laydate.render({
            elem: '#start_time',
            type: 'datetime'

          });
        });
        layui.use('laydate', function(){
          var laydate = layui.laydate;
          laydate.render({
            elem: '#end_time',
            type: 'datetime'

          });
        });

    },
    initEvent: function(){
        var _this = this;
        if (_this.params.slideEdit==1) {
            _this.getData();
        };
         _this.otherEvent();
         _this.submit();
        _this.uploadImg();

    }
};
var page = new Page();